package cn.wawi.controller.business;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import cn.wawi.common.annotation.Permission;
import cn.wawi.common.interceptor.AgentTokenInterceptor;
import cn.wawi.common.interceptor.SellerTokenInterceptor;
import cn.wawi.controller.BaseController;
import cn.wawi.model.business.ConsumRecord;
import cn.wawi.model.business.Member;
import cn.wawi.model.business.Memberservice;
import cn.wawi.model.business.Packet;
import cn.wawi.model.business.Payrecord;
import cn.wawi.model.business.Serviceitem;
import cn.wawi.model.sys.User;
import cn.wawi.service.MemberServiceI;
import cn.wawi.utils.DateUtil;

import com.jfinal.aop.Before;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.render.JsonRender;
import com.mchange.v2.lang.StringUtils;

@ControllerBind(controllerKey="/member")
public class MemberController extends BaseController<Member>{
	private MemberServiceI memberService = new MemberServiceI();
	
	//生成一批二维码
	
	
	//修改二维码
	
	
	//会员审核(后台)
	
	//会员充值(手机端)...
	
	//销售人员()...
	
	//后台子帐号列表
	
	//消费
	
	//员工token

	private Integer addMemberQ(){
		Model<Member> m=getModel(Member.class);
		m.set("createTime", new Date());
		m.set("userId", getPara("managerId"));
		m.remove("money");
		m.remove("totalCount");
		m.remove("status");
	    m.remove("checkStatus");
	     m.save();
	     return m.getInt("id");
	}
	
	
	public void zhichong(Integer userId, Integer memberId, BigDecimal money, Integer count, Integer time,
			Integer serviceId, String serviceName) {
		try {
			memberService.member_chongzhi_zhi(userId, memberId, money, count, time, serviceId, serviceName);
		}catch(Exception e){
			e.printStackTrace();
		}
		render(new JsonRender(json).forIE());
	}

	
	//开户并且充值
	@Before({Tx.class})
	public void addMemberWithMoney(){
	    try{
	    	String packetIds = getPara("packetIds");
			String userId= getPara("managerId");
			String code = getRequest().getParameter("code");
			if(StringUtils.nonEmptyString(code))
			{
			List l =   Db.find("select * from member where code = ?",code);	
				if(l.size()>0)
				{
					json.setResMsg("二维码已经被使用，请更换");
					json.setResCode(0);
				    render(new JsonRender(json).forIE());
		            return ;  	
				}			
			}
	    	Integer memberId = addMemberQ();
	    	taocanchong(packetIds, Integer.valueOf(userId), memberId);
		   // addPacketWithMember(packetIds,userId,memberId);
	    }catch(Exception e){
	    	e.printStackTrace();
	    	json.setResMsg("登记失败!"+e.getMessage());
			json.setResCode(0);
		    render(new JsonRender(json).forIE());
            return ;
	    }
	    json.setResMsg("登记成功!");
		json.setResCode(1);
	    render(new JsonRender(json).forIE());
        return ;
	}
	
	
	
	
	/**
	 * 销售人员查看会员具体信息
	 */
	@Before(SellerTokenInterceptor.class)
	public void findMemberById(){
		//查询会员信息
		Member member = new Member();
		member = member.findById(getPara("memberId"));
		json.setRow(member);
	    render(new JsonRender(json).forIE());
	}	
	
	
	
	
	/**
	 * 扫描二维码获取会员信息
	 */
	//@Before(SellerTokenInterceptor.class)
	public void  findMemberByCode(){
	   String  code = getRequest().getParameter("code");
	   Member member =   Member.dao.findFirst(" select *  from member A  where code =? ",code);
	   if(member==null){
		   Map map = new HashMap<String, Object>();
		   json.setRow(map);
	   }else{
		   json.setRow(member);
	   }
       render(new JsonRender(json).forIE());
	}
	
	/**
	 * 商户扫描会员二维码获取用户信息
	 */
	//@Before(AgentTokenInterceptor.class)
	public void getMember(){
	   String  code = getRequest().getParameter("code");
	   String serviceId=getRequest().getParameter("serviceId");
	   Member member =   Member.dao.findFirst(" select x.*,y.time from (select *  from member A  where code =?  ) x  left join (select * from memberservice B where serviceId =?) y on x.id = y.memberid; ", code,serviceId);
	   
	   json.setRow(member);
       render(new JsonRender(json).forIE());
	}
	
	//更新二维码
	@Before(SellerTokenInterceptor.class)
	public void updateMemberQRcode(){
		
		String code = getRequest().getParameter("code");
		if(StringUtils.nonEmptyString(code))
		{
		List l =   Db.find("select * from member where code = ?",code);	
			if(l.size()>0)
			{
				json.setResMsg("二维码已经被使用，请更换");
				json.setResCode(0);
			    render(new JsonRender(json).forIE());
	            return ;  	
			}			
		}
		int x = Db.update("update member set code=? where userid=? and id =? ", getPara("code"),getPara("managerId"),getPara("id"));
		if(x==0){
			json.setResMsg("失败!");
			json.setResCode(0);
		}
        render(new JsonRender(json).forIE());
	}
	
	
	
	
	//我的会员
	@Before(SellerTokenInterceptor.class)
	public void findMyMember(){
		StringBuilder sql = new StringBuilder("select * from member A where A.deleted!=1 and A.userId= ? ");		
		String managerId = getRequest().getParameter("managerId"); //员工Id
		String beginDateTime = getRequest().getParameter("beginDateTime");
		String endDatetime = getRequest().getParameter("endDateTime");
		List lt = new ArrayList();
		lt.add(getRequest().getParameter("managerId"));
		
		if(beginDateTime!=null )
		{
		    String start =	DateUtil.formatDateTime(new Date(Long.valueOf(beginDateTime)));
			sql.append(" and A.createTime > ?" );
            lt.add(start);
		}
		
		if(endDatetime!=null){
			   String end =	DateUtil.formatDateTime(new Date(Long.valueOf(endDatetime)));
				sql.append(" and A.createTime < ? " );
               lt.add(end);
		}		
		
		if(StringUtils.nonEmptyString(getRequest().getParameter("checkStatus"))){
			sql.append(" and A.checkStatus = ? ");
			lt.add(getRequest().getParameter("checkStatus"));
		}

		Page<Member> list=getModel(Member.class).paginateMysql(toPageIndex(), toPageSize(),sql.toString(),lt.toArray());
		
	    List<Record> records =	Db.find("select count(*) as totals, checkStatus  from ("+sql.toString()+")X  group by  checkStatus ",lt.toArray());;
		Map<String, Object> map = new HashMap<String, Object>();
    	map.put("unCheckedNum", 0);
    	map.put("checkedNum", 0);
    	map.put("rejectNum", 0);
        for(Record record:records){
        	if(0==record.getInt("checkStatus"))
        	{
            	map.put("unCheckedNum", record.getLong("totals").intValue());

        	}
        	
        	if(1==record.getInt("checkStatus"))
        	{
            	map.put("checkedNum", record.getLong("totals").intValue());

        	}
        	
        	if(2==record.getInt("checkStatus"))
        	{
            	map.put("rejectNum", record.getLong("totals").intValue());

        	} 	
        }
	    json.setRow(map);
		json.setPageNumber(list.getPageNumber());
		json.setRows(list.getList());
		json.setTotal(list.getTotalRow()+0L);
		//审核  位审核     拒绝      时间段
		//充值多少钱  多少点   多少次
        render(new JsonRender(json).forIE());
	}
	
	
	//修改会员
	@Before(SellerTokenInterceptor.class)
	public void updateMember()
	{
		
		//TODO 审核了的不允许修改 
		//二维码不验证
		Model<?> m=getModel(Member.class);
	    Record rec = Db.findFirst("select * from member where id = ?" ,m.get("id"));
		if(rec.getInt("checkStatus")==1)
		{
			json.setResMsg("用户已经通过审核，不能被修改");
			json.setResCode(0);
			render(new JsonRender(json).forIE());	
            return ;
		}
	    m.remove("name");
		m.remove("money");
	    m.remove("totalCount");
	    m.remove("status");
	    m.remove("checkStatus");
		if(!m.update()){
			json.setResMsg("修改失败!");
			json.setResCode(0);
		}
		render(new JsonRender(json).forIE());	
	}
	
	//删除会员
	@Before(SellerTokenInterceptor.class)
	public void deleteMember()
	{
		   //删除未审核的
		int n =   Db.update("update member set deleted = 1 where id = ? and userId =? and checkStatus=0 ",getRequest().getParameter("memberId"),getRequest().getParameter("managerId"));
		if(n<1){
			json.setResMsg("只能删除未审核的会员");
			json.setResCode(0);
		}
	}
	
	
	
	/**
	 * 通用新增
	 */
	@Permission("add")
	public void addOne(){	
		 UUID uuid = UUID.randomUUID();
		Model<Member> m=getModel(Member.class);
		m.set("code","member_"+uuid.toString() );
		User user=getSessionAttr("loginUser");
        m.set("userId", user.getId());
		if(!m.save()){
			json.setResMsg("添加失败!");
			json.setResCode(0);
		}
		render(new JsonRender(json).forIE());
	}
    
	
	
	/**
	 public void openService(){
		String memberid=getPara("memberid");
		Db.update("delete from memberservice where memberid=?", memberid);
		String privilegeIds=getPara("serviceids");
		if(privilegeIds!=null&&!"".equals(privilegeIds.trim())){
			String[] privileges=privilegeIds.split(",");
			Object[][] o=new Object[privileges.length][2];
			for (int i = 0; i < o.length; i++) {
				o[i][0]=memberid;
				o[i][1]=privileges[i];
			}
			Db.batch("insert into memberservice(memberid,serviceid) values(?,?)",o, 1000);
		}
		render(new JsonRender(json).forIE());
	}
	
	public void loadOpenService(){
		render(new JsonRender(Db.find("select id,name namex from serviceitem where id in(select SERVICEid from memberservice where MEMBERID =?) and deleted !=1 ",getRequest().getParameter("mid"))).forIE());
	}
	*/
	
	/**
	 * 会员审核
	 */
	@Permission("checkMember")
	public void validateMember(){
		System.out.println(getRequest().getParameter("mid"));
	    int mid =    getParaToInt("mid");
        int cst=  getParaToInt("checkStatus");
		if(cst==1)
		{
			Db.update("update member set checkStatus=1 , remarker=?,status=1 where id =? ",getPara("remarker"),mid);
		}else{
			Db.update("update member set checkStatus=? ,remarker=? where id =? ",cst,getPara("remarker"),mid);	
		}
		render(new JsonRender(json).forIE());
        
	}
	
	/**
	 * 会员充值礼包
	 */
	@Before({Tx.class})
    public void addPacketToMember(){
		String packetIds = getPara("packetIds");
		String userId= getPara("managerId");
		Integer memberId =  Integer.valueOf(getRequest().getParameter("memberId"));
		try{
		  taocanchong(packetIds,Integer.valueOf(userId), memberId);
			//	addPacketWithMember(packetIds,userId,memberId);
		}catch(Exception e){
			e.printStackTrace();
		}
    	json.setResMsg("充值成功!");
		json.setResCode(1);
		render(new JsonRender(json).forIE());
	}
	
	@Before(Tx.class)
	public void addPacketToMemberInBackGround()
	{
		String packetIds = getPara("packetIds");
		User user=getSessionAttr("loginUser");
		String userId= String.valueOf(user.getId());
		Integer memberId =  Integer.valueOf(getRequest().getParameter("memberId"));
		try{
			taocanchong(packetIds, user.getId(), memberId);
			//addPacketWithMember(packetIds,userId,memberId);
		}catch(Exception e){
			e.printStackTrace();
		}
		json.setResMsg("充值成功!");
		json.setResCode(1);
		render(new JsonRender(json).forIE());
	}
	
	private void addPacketWithMember1(String packetIds,String userId,Integer  memberId) throws Exception{
		try{
			    
		        Payrecord order = new Payrecord();
			    Date date = new Date();
		        order.setCreateTime(date);
		        order.setMemberId(memberId);
		        order.setUserId(Integer.valueOf(userId));
                BigDecimal ordermoney =  new  BigDecimal(0);
                Integer orderTimes =  new  Integer(0); 			    
                Integer orderCounts =  new  Integer(0); 			    
                order.save();
				String[] packetIdArry  =   packetIds.split(",");
				for(String packetId:packetIdArry)
				{
			        
					 Packet  pak= Packet.dao.findById(packetId);
					 
					 Record cRecord = Db.findFirst("select sum(time) as times, sum(count) as counts from packetitem where id = ? and deleted !=1",packetId);
					 //套餐记录
					 Payrecord packetPayRecord = new Payrecord();
					 packetPayRecord.setMoney(pak.getMoney());
					 packetPayRecord.setMemberId(memberId);
					 packetPayRecord.setPacketName(pak.getName());
					 packetPayRecord.setPacketRemarkerSnapshot(pak.getRemarker());
					 packetPayRecord.setProportion(pak.getProportion());
					 packetPayRecord.setUserId(memberId);
					 packetPayRecord.setCreateTime(date);
					 packetPayRecord.setPacketId(Integer.valueOf(packetId));
					 packetPayRecord.setCount(cRecord.getBigDecimal("counts").intValue());
					 packetPayRecord.setTime(cRecord.getBigDecimal("times").intValue());
                     packetPayRecord.setParentId(order.getId());  //父Id,  //订单
					 packetPayRecord.save();
					 ordermoney.add(pak.getMoney());
					 orderTimes+=packetPayRecord.getTime();
					 orderCounts+=packetPayRecord.getCount();
					 //子记录
					 List<Record> records =  Db.find("select * from packetitem where id = ? and deleted !=1 ",packetId); 
				        for(Record mrecord:  records)
				        {   
				          	 Payrecord p = new Payrecord();
				          	 p.setUserId(Integer.valueOf(userId));
				        	 p.setMemberId(memberId);
				        	 p.setCreateTime(date);
				        	 p.setPacketId(Integer.valueOf(packetId));
				        	 BigDecimal money = mrecord.getBigDecimal("money");
				     		 p.setMoney(money);
				     		Integer serviceId =  mrecord.getInt("serviceId");
				     		Serviceitem sitem = Serviceitem.dao.findById(serviceId);
				     		if(sitem!=null){
					        	p.setServiceName(sitem.getName());
				     		}
                            p.setParentId(packetPayRecord.getId());
				        	 if(serviceId ==null)
				        	{   
				        		p.setCount(mrecord.getInt("count"));
				        		Member record = Member.dao.findById(memberId); //用户金钱
				                BigDecimal newbig= record.getBigDecimal("money").add(money);
				                p.save();
				                Db.update("update member set totalCount = totalCount+ "+mrecord.getInt("count")+" ,money=? where id = ? ",newbig,memberId);
				        	}else{
				        		p.setTime(mrecord.getInt("time"));
				        		p.setServiceId(serviceId);
				        		p.save();
				        		Member record = Member.dao.findById(memberId); //用户金钱
				                BigDecimal newbig= record.getBigDecimal("money").add(money);        		
				                Db.update("update member set money=? where id = ? ",newbig,memberId);
				        		Memberservice ms = new Memberservice();
				        		ms = ms.findFirst("select * from memberservice where memberid=? and serviceid = ? ", memberId,serviceId);
				        		if(ms !=null)
				        		{
				        			Db.update("update memberservice set time = time + "+p.getTime()+" where memberid=? and serviceid = ?  ",memberId,serviceId);		
				        		}else{
				        			ms = new Memberservice();
				        			ms.setCreateTime(date);
				        			ms.setMemberid(memberId);
				        			ms.setServiceid(serviceId);
				        			ms.setTime(p.getTime());
				        			ms.save();
				        		}
				        		
				        		
				        	}
				        }	
				}
				order.update();
				
				
		}catch(Exception e){
			e.printStackTrace();
			 throw new RuntimeException(e.getMessage());
		}
	}
	
	
	
    
	@Before(Tx.class)
	public void addMoney(){
		getRequest().getParameter("user");
		User user=getSessionAttr("loginUser");
		Model<Payrecord> m=getModel(Payrecord.class);
        m.set("userid", user.getId());
        String memberid = String.valueOf(m.get("memberid"));
        Record record = Member.dao.getMemberById(memberid); //用户金钱
        BigDecimal allmoney =  record.get("money");
        allmoney.add(new BigDecimal((String)m.get("money"))); //金钱
        Integer totalcount = record.get("totalcount");
        String count =  m.get("count");
        m.save();
        Db.update("update member set totalcount = ? ,money=? where id = ? ",totalcount+count,allmoney,memberid);
    	json.setResMsg("充值成功!");
		json.setResCode(0);
		render(new JsonRender(json).forIE());
	}
	
	
	
	
	@Before({Tx.class,AgentTokenInterceptor.class})
	public void cosume(){
		//会员二位码  - >会员id
		//商户开通服务   用户拥有这个服务
		//商户ID
		//服务ID
		//扣点数
		//扣次数
		try{
			   String memberid = getRequest().getParameter("memberId");  //会员Id  
			   Integer agentid = Integer.valueOf(getRequest().getParameter("agentId")); //代理商员工Id
			   Integer serviceid = Integer.valueOf(getRequest().getParameter("serviceId")); //服务项目Id
			   
			   //会员
			   Record record_member = Db.findFirst("select SERVICEid,time from memberservice where MEMBERID =? and serviceid=? ",memberid,serviceid);
			   //会员的充值次数记录
			   Record memberInfo = Db.findById("member", memberid);
			   int chekst= memberInfo.getInt("checkStatus");
			   if(chekst!=1){
				   
				   if(chekst==0){
					   json.setResMsg("用户待审核,暂时不能消费");
				   }
				   
				   if(chekst==2)
				   {
					   json.setResMsg("用户资料被拒绝,暂时不能消费");
				   }
				   
				   if(chekst==3)
				   {
					   json.setResMsg("帐号被锁定，暂时不能消费");
				   }
				   
				   json.setResCode(0);
				   render(new JsonRender(json).forIE());
			       return;   
			   }
			   ConsumRecord record = new ConsumRecord();   
			   //检测商户是否开通此服务
			   Record as=  Db.findFirst("select name,parent_id from agents_customer where id = ?",agentid);
			   Record record_agents = null;
			   if(as.getStr("parent_id")==null){
				   record_agents = Db.findFirst("select * from agentsservice where agentsid=? and serviceid = ?  ",agentid,serviceid);
				   record.setApId(agentid);
			   }else{
				   record_agents= Db.findFirst("select * from agentsservice where agentsid=? and serviceid = ?  ",as.getStr("parent_id"),serviceid);
				   record.setApId(Integer.valueOf(as.getStr("parent_id")));

				   String asn = Db.findById("agents_customer", as.getStr("parent_id")).get("name");
				   as.set("name",asn);

			   }
			   if(record_agents==null){
				   json.setResMsg("商户未开通此服务");
				   json.setResCode(0);
				   render(new JsonRender(json).forIE());
			       return;
			   }
			   Date date = new Date();
			   Record serviceItem =  Db.findById("serviceitem", "id", serviceid);
			   int times = record_member==null ?0: record_member.getInt("time"); //会员拥有的服务次数
			   String payway =serviceItem.get("payway");
			   Integer price = serviceItem.get("singleprice");
			   record.setCreateTime(date);
			   record.setAgentId(agentid);
			   record.setMemberId(Integer.valueOf(memberid));
			   record.setServiceId(serviceid);
			   if(payway.contains("1")&&price==0)
			   {
				   //免费
		           record.setCount(0);  //消费0点
		           record.setPayway("1"); //按点数支付
		           record.setMessage("系统赠送! 您成功在"+DateUtil.formatDate(date,"yyyy-MM-dd HH:mm:ss")+ " 在商家   ["+as.getStr("name")+ "]  消费一次 [ "+serviceItem.get("name")+" ]服务");
		           if(record.save()){
		        	   json.setResMsg("免费服务!");
		    		   json.setResCode(1);
		    		   render(new JsonRender(json).forIE());
		           }else{
		        	   json.setResMsg("支付失败!,请联系服务员");
		    		   json.setResCode(1);
		    		   render(new JsonRender(json).forIE());
		           }
			   }else{
				   
			     if(payway.equals("2")){
			    	   if(times==0){
			    		   json.setResMsg("次数为0，请充值");
						   json.setResCode(0);
						   render(new JsonRender(json).forIE());
					       return;
			    	   }else{
			    		   //扣次数
			    		   record.setTime(1);;  //消费0点
			               record.setPayway("2"); //按点数支付
				           record.setMessage("您成功在"+DateUtil.formatDate(date,"yyyy-MM-dd hh:mi:ss")+ " 在商家   ["+as.getStr("name")+ "]  消费一次 [ "+serviceItem.get("name")+" ]服务");
			               record.save();
			                Db.update("update memberservice set time = time-1   where memberid = ?,serviceid = ?  ",memberid,serviceid);
		                   json.setResMsg(record.getMessage());
						   json.setResCode(1);
						    render(new JsonRender(json).forIE());
			    	       return ;	   
			    	   }
			    	  
			     }
			     
			     if(payway.equals("1"))
			     {
			    	 //只支持点数
			    	 if(price>memberInfo.getInt("totalCount"))
			    	 {
			    		     // 点数不够，请充值
			    		   json.setResMsg("点数不足，请充值");
						   json.setResCode(0);
						   render(new JsonRender(json).forIE());
					       return;  
			    	 }else{
			    		
			    		 //扣次数
			    		   record.setCount(price);  //消费0点
			               record.setPayway("1"); //按点数支付
				           record.setMessage("您成功在  "+DateUtil.formatDate(date,"yyyy-MM-dd HH:mm:ss")+ "  在商家   ["+as.getStr("name")+ "]  消费一次  ["+serviceItem.get("name")+"] 服务 ");
			               Db.update("update  member set totalcount= totalcount-"+price+" where id = ?", memberid);
			               record.save();
		                 
			    		 json.setResMsg(record.getMessage());
						 json.setResCode(1);
						 render(new JsonRender(json).forIE());
			    		 
			    	     return ;
			    	 }
			    }
			     
			     
			    if(payway.contains("1, 2"))
			    {
			    	//既支持点数，又支持次数，先扣次数..
			    	
			    	if(times>0)
			    	{
			    		//先扣次数。。
			    		  record.setTime(1);;  //消费0点
			              record.setPayway("2"); //按点数支付
				          record.setMessage("您成功在 "+DateUtil.formatDate(date,"yyyy-MM-dd HH:mm:ss")+ " 在商家   ["+as.getStr("name")+ "]  消费一次 ["+serviceItem.get("name")+"] 服务,剩余次数"+(times-1));
			              record.save();
			              Db.update("update memberservice set time = time-1   where memberid = ? and serviceid = ?  ",memberid,serviceid);
		                  json.setResMsg(record.getMessage());
						   json.setResCode(1);
						    render(new JsonRender(json).forIE());
			    	       return ;	   
			    		
			    	}
			    	
			    	
			    	if(Integer.valueOf(price)>memberInfo.getInt("totalCount"))
			    	 {
			    		     // 点数不够，请充值
			    		   json.setResMsg("余额不足，请充值");
						   json.setResCode(0);
						   render(new JsonRender(json).forIE());
					       return;  
			    	 }else{
			    		 //扣点数
			    		   record.setCount(price);  //消费0点
			               record.setPayway("1"); //按点数支付
				           record.setMessage("您成功在"+DateUtil.formatDate(date,"yyyy-MM-dd HH:mm:ss")+ " 在商家   ["+as.getStr("name")+ "]  消费一次 ["+serviceItem.get("name")+"] 服务,使用点数  "+ price +"点,剩余点数"+(memberInfo.getInt("totalCount")-price));
			               Db.update("update  member set totalcount= totalcount-"+price+" where id = ?", memberid);
			               record.save();
			    		 json.setResMsg(record.getMessage());
						 json.setResCode(1);
						 render(new JsonRender(json).forIE());
			    	     return ;
			    	 }   	
			    }	   
				   //会员拥有的点数
			   }
			   
		}catch(Exception e){
			e.printStackTrace();
			 json.setResMsg("消费失败!");
			 json.setResCode(0);
			 render(new JsonRender(json).forIE());
			 return ;
		}   
	}

	
	
	public void taocanchong(String packetIds,Integer userId,Integer memberId) {
		try {
		  memberService.member_chongzhi_packet(packetIds, userId, memberId);
		}catch(Exception e){
			e.printStackTrace();
		}	
		
	}

	
	
	
	/*
	 * 会员开通PC版服务
	 */
	@Before(Tx.class)
	public void saveMemberService(){
		String memberid=getPara("memberid");
		Db.update("delete from memberservice where memberid=?", memberid);
		String privilegeIds=getPara("privilegeIds");
		if(privilegeIds!=null&&!"".equals(privilegeIds.trim())){
			String[] privileges=privilegeIds.split(",");
			Object[][] o=new Object[privileges.length][2];
			for (int i = 0; i < o.length; i++) {
				o[i][0]=memberid;
				o[i][1]=privileges[i];
			}
			Db.batch("insert into memberservice(memberid,serviceid) values(?,?)",o, 1000);
		}
		render(new JsonRender(json).forIE());
	}
	
}
